package com.shoulder.authorizationserver.rsa;

import java.util.*;
public class InMemoryRsaKeyPairRepository implements RsaKeyPairRepository {

    private final Map<String, RsaKeyPair> idToKeyPair = new HashMap<>();

    @Override
    public List<RsaKeyPair> findKeyPairs() {
        List<RsaKeyPair> result = new ArrayList<>(this.idToKeyPair.values());
        result.sort(Comparator.comparing(RsaKeyPair::getCreated).reversed());
        return result;
    }

    @Override
    public void delete(String id) {
        this.idToKeyPair.remove(id);
    }

    @Override
    public void save(RsaKeyPair rsaKeyPair) {
        this.idToKeyPair.put(rsaKeyPair.getId(), rsaKeyPair);
    }

    @Override
    public RsaKeyPair findById(String id) {
        return this.idToKeyPair.get(id);
    }

}